package com.example.bookmanage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.bookmanage.domain.book.Book;
import com.example.bookmanage.domain.book.dto.BookDTO;
import com.example.bookmanage.domain.book.dto.BookEditDTO;
import com.example.bookmanage.domain.book.dto.BookQueryDTO;
import com.example.bookmanage.domain.book.vo.BookVO;
import com.example.bookmanage.mapper.BookMapper;
import com.example.bookmanage.service.BookService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lyk19
 * @description 针对表【book_db】的数据库操作Service实现
 * @createDate 2025-02-01 20:59:02
 */
@Service
@Slf4j
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 分页查询
     *
     * @param bookQueryDTO
     * @return
     */
    public List<BookVO> list(@ParameterObject BookQueryDTO bookQueryDTO) {
        PageHelper.startPage(bookQueryDTO.getPageNum(), bookQueryDTO.getPageSize());

        return bookMapper.selectBookList(bookQueryDTO);
    }

    /**
     * 添加图书
     *
     * @param bookDTO
     * @return
     */
    public int addBook(BookDTO bookDTO) {

        Book book = new Book();

        BeanUtil.copyProperties(bookDTO, book);

        return bookMapper.insert(book);
    }

    /**
     * 修改图书
     *
     * @param BookEditDTO
     * @return
     */
    public int updateBook(BookEditDTO BookEditDTO) {
        Book book = new Book();
        BeanUtil.copyProperties(BookEditDTO, book);
        return bookMapper.updateById(book);
    }

    /**
     * 删除图书
     *
     * @param bookId
     * @return
     */
    public int deleteById(Long bookId) {
        return bookMapper.deleteById(bookId);
    }
}
